package com.ruoyi.system.artrec.service.impl;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.artrec.service.ArtRecService;
import com.ruoyi.system.model.domain.ArtRec;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @other pzz
 * @data 2024/8/23
 * @apiNote
 */
@Service
public class ArtRecServiceImpl implements ArtRecService {
    @Autowired
    private ArtRecMapper artRecMapper;

    @Override
    public List<ArtRec> selectArtRecList(ArtRec artRec) {
        List<ArtRec> artRecs = artRecMapper.selectArtRecListTwo(artRec);
        return artRecs;
    }

    @Override
    public int update(ArtRec artRec) {
        if (Objects.isNull(artRec.getOutid())) {
            throw new GlobalException("三方id不可为空");
        }
        artRec.setUpdateTime(new Date());
        return artRecMapper.updateArtRec(artRec);
    }

    /**
     * 交易猫手动新增outId
     * @param artRec
     * @return
     */
    @Override
    public int insert(ArtRec artRec) {
        if (Objects.isNull(artRec.getAccountId())) {
            throw new GlobalException("商品id不可为空");
        }
        if (Objects.isNull(artRec.getConfig())) {
            throw new GlobalException("交易猫三方id不可为空");
        }
        if (Objects.isNull(artRec.getOutid())) {
            throw new GlobalException("三方id不可为空");
        }
        ArtRec rec = new ArtRec();
        rec.setAccountId(artRec.getAccountId());
        rec.setConfig(artRec.getConfig());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(rec);
        if (CollectionUtils.isNotEmpty(artRecs)) {
            throw new GlobalException("已存在" + artRecs.get(0).getSaleCode());
        } else {
            artRec.setCreateTime(new Date());
            artRec.setType("add");
            artRec.setCreateId(SecurityUtils.getUserId());

            artRecMapper.insertArtRec(artRec);
        }
        return 1;
    }

    @Override
    public int delete(Long[] ids) {
        return artRecMapper.deleteArtRecByIds(ids);
    }
}
